﻿#encoding: utf-8
#this file can be used in other ruby file by loading 'wechat_api.rb'

require 'rest-client'   #to use RestClient
require 'json'          #to use JSON
require 'awesome_print' #to use ap helper

load 'wechat_api.rb'
load 'rds_api.rb'
@rds = @rds_tt

def sendSMS phone='13600060044', template = 'PRODUCT_WITHDRAW_NOTICE',
            data = {'name' => '董学锋', 'product'=> '小儿拾和大豆肽产品', 'admin' => '神经科华华' },
            weapp_args = { "path" => "/pages/news_details/index", "query"=> "id=1129&spreader_uid=736", "is_expire" => false }
    param = {   'api_key'=>ENV['PSI_PASSWORD'],
                'template' => template,
                'phone' => phone,
                'weapp_args' => weapp_args, #如果weapp_args非空，且根据不同的模板将替代data中的'admin'等关键值
                'data' => data
            }
    url = 'https://mall.totembio.cn/api/send_sms'
    begin
        res = RestClient.post url, param.to_json, :content_type => 'application/json'
    rescue=>e
        puts "ERROR: #{e.to_s}"
        return false
    end
    puts res.to_s
    return true

end
#sendSMS

#发送短信给所有购买过小儿拾和大豆肽的用户
def pwn20230223 #product withdraw notice
    sql = "select uid, user_phone as phone, real_name as name from tutem_mall.eb_store_order where paid = 1 and tags like '%XESH%' or tags like '%DDT%' group by phone"
    recs = {}
    puts "sending sms"
    (queryRds(sql)).each do |s|
        name = s['name']
        phone = s['phone']
        param = {   'api_key'=>ENV['PSI_PASSWORD'],
                    'template' => 'PRODUCT_WITHDRAW_NOTICE', 'phone' => phone,
                    'weapp_args' => { "path" => "/pages/news_details/index", "query"=> "id=1129&spreader_uid=736", "is_expire" => false },
                    'data' => {'name' => name, 'product'=> '小儿拾和大豆肽产品', 'admin' => '' }
                }
        sendSMS
    end
    puts "\ndone [#{recs.size}]"
end
#pwn20230223

#用于给所有具有电话号码且不在积分中奖名单也不在催积分表上的用户发放积分并发送短信
def sms20230928 phone='13600060044'
    template = 'ADD_POINTS'
    data = { 'gain_integral' => '199', 'integral' => '可中秋抽奖 https://mta.totembio.cn/apps/a/ 祈愿您家和人旺' }
    #data = { 'date' => '今天', 'phone' => 'M1', 'pwd' => 'xxxx', 'site_name' => '图腾' }
    #data = { 'order_id' => 'sfsdf', 'refund_price' => '8888.88' }
    weapp_args = {}
    sendSMS phone, template, data, weapp_args
end

def survey20240616
    sql = "select uid, user_phone as phone, real_name as name from tutem_mall.eb_store_order where paid = 1 and uid = 1 group by phone"
    counter = 0
    puts "sending sms"
    (queryRds(sql)).each do |s|
        counter += 1
        phone = s['phone']
        name = s['name']
        weapp_args = {}
        template1 = 'SURVEY'
        data1 = {'user_name' => name, 'survey_name'=> '2024上半年使用随访', 'rep_name' => ' totemlife.cn/apps/e' }
        template2 = 'SURVEY2'
        data2 = {'bonus' => '1999', 'rep_name' => ' totemlife.cn/apps/e' }
        #sendSMS phone, template1, data1, weapp_args
        sendSMS phone, template2, data2, weapp_args
        exit
    end
    puts "\ndone [#{counter}]"
end
